<?php

namespace app\admin\controller;


use app\common\service\ManagerToken;

class Login extends BaseController {
    public function check() {
        $data = input('post.');
        $user = model('AdminUser')->get(['username' => $data['username']]);
        if (!$user) {
            return show(config('code.fail'), '用户名不存在');
        }
        if (!empty($data['passwd'])) {
            $data['passwd'] = md5(md5($data['passwd']) . $data['salt']);
            if ($data['passwd'] !== $user->password) {
                return show(config('code.fail'), '密码不正确');
            } else {
                $adata = [
                    'last_login_time' => time(),
                    'last_login_ip'   => request()->ip()
                ];
                model('AdminUser')->save($adata, ['id' => $user->id]);
                return show(config('code.success'), 'ok', ['token' => ManagerToken::saveToCache($user)]);
            }
        }
        return show(config('code.success'), 'OK');
    }
}